package com.haowang.camera.module;

import com.haowang.camera.R;
import com.haowang.camera.activity.CameraActivity;
import com.haowang.camera.manager.CameraManager;
import com.haowang.camera.manager.SurfaceHorderPreviewManager;
import com.haowang.camera.manager.SurfaceTexturePreviewManager;
import com.haowang.camera.util.ApiHelper;
import com.haowang.camera.util.GlobalValues;
import com.haowang.camera.util.Util;

import android.content.res.Configuration;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout.LayoutParams;

public class PhotoModule extends CameraModule implements OnClickListener {

	private static final String TAG = PhotoModule.class.getSimpleName();
	private PictureCallback mJpegPictureCallback = new JpegCallBack();

	public PhotoModule(CameraActivity context) {
		super(context);
	}

	@Override
	public void init() {
		super.init();
		mActivity.getLayoutInflater().inflate(R.layout.photo_module,
				(ViewGroup) mActivity.getMainFrame());
		if (mShutterButton == null) {
			mShutterButton = (ImageButton) mActivity
					.findViewById(R.id.shutter_button);
			mShutterButton.setOnClickListener(this);
		}
		
		if (ApiHelper.HasSurfaceTexture()) {
			mPreviewManager = new SurfaceTexturePreviewManager(mActivity);
		} else {
			mPreviewManager = new SurfaceHorderPreviewManager(mActivity);
		}
		mPreviewManager.init();
		mPreviewManager.setLayout(GlobalValues.MODE_PHOTO);
		LayoutParams sbLP = (LayoutParams) mShutterButton.getLayoutParams();

		sbLP.bottomMargin = 10;
		sbLP.height = mPreviewManager.getBottomMargin() - sbLP.bottomMargin;
		sbLP.width = sbLP.height;

		mShutterButton.setLayoutParams(sbLP);
	}

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onFullScreenChanged(boolean full) {
		// TODO Auto-generated method stub
		super.onFullScreenChanged(full);
	}

	@Override
	public void onConfigurationChanged(Configuration config) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(config);
	}

	@Override
	public void startPreview(boolean forceStop) throws Exception {
		if (forceStop) {
			try {
				stopPreview();
			} catch (Exception e) {
				// ingore error
			}
		}
		CameraManager.instance().startPreview();
	}

	@Override
	public void initParameters() {
		mParameters = mCameraDevice.getParameters();

		for (Size s : mParameters.getSupportedPreviewSizes()) {
			// check ratio
			if (Util.checkRatio(s)) {
				mParameters.setPreviewSize(s.width, s.height);
				break;
			}
		}

		for (Size s : mParameters.getSupportedPictureSizes()) {
			if (Util.checkRatio(s)) {
				mParameters.setPictureSize(s.width, s.height);
				break;
			}
		}

		mParameters.setPictureFormat(ImageFormat.JPEG);
		mCameraDevice.setDisplayOrientation(90);
		mCameraDevice.setParameters(mParameters);
		super.initParameters();
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.shutter_button:
			capture();
			break;

		default:
			break;
		}

	}

	private void capture() {
		Log.i(TAG, "capture");
		CameraManager.instance().takePicture(null, null, null,
				mJpegPictureCallback);
	}

	class JpegCallBack implements PictureCallback {

		@Override
		public void onPictureTaken(byte[] data, Camera camera) {
			Log.i(TAG, "onPictureTaken");

			try {
				startPreview(false);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
